package pl.edu.wsinf.lukaszwalczak.emailclient.view;

import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.ToolBar;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.controller.ContactCreateController;
import pl.edu.wsinf.lukaszwalczak.emailclient.controller.ContactListController;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Contact;

/**
 * Widok wyświetlający okno z listą kontaktów.
 */
@Component
public class ContactListView {

    @Inject
    private ContactCreateController contactCreateController;
    @Inject
    private ContactListController contactListController;
    private Stage stage = null;
    private TableView tableView;

    public void show() {
        if (stage == null) {
            stage = new Stage();
            //resizable disabled
            stage.setResizable(false);
            //create root node of scene, i.e. group
            BorderPane root = new BorderPane();
            //create scene with set width, height and color
            Scene scene = new Scene(root, 700, 400, Color.WHITESMOKE);
            //set scene to stage
            stage.setScene(scene);
            stage.setTitle("Kontakty");
            //center stage on screen
            stage.centerOnScreen();
            // stage icon
            Image contactsIcon = new Image(MainWindowView.class.getResourceAsStream("/icons/addressbook-16.png"));
            stage.getIcons().add(contactsIcon);

            // Toolbar
            ToolBar toolbar = new ToolBar();
            Button newContactButton = ViewHelper.createButton("Dodaj", new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    contactCreateController.showWindow();
                }
            }, "address-book-add-16.png", null, 24d);

            Button deleteContactButton = ViewHelper.createButton("Dodaj", new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    ObservableList<TablePosition> selections = tableView.getSelectionModel().getSelectedCells();
                    for (TablePosition tablePosition : selections) {
                        Contact contact = (Contact) tableView.getItems().get(tablePosition.getRow());
                        contactListController.removeContact(contact);
                    }
                    contactListController.loadList();
                }
            }, "address-book-delete-16.png", null, 24d);

            toolbar.getItems().addAll(newContactButton, deleteContactButton);
            root.setTop(toolbar);

            // Tabela
            TableColumn labelColumn = new TableColumn();
            labelColumn.setText("Nazwa");
            labelColumn.setCellValueFactory(new PropertyValueFactory("label"));

            TableColumn nameColumn = new TableColumn();
            nameColumn.setText("Imię");
            nameColumn.setCellValueFactory(new PropertyValueFactory("name"));

            TableColumn surnameColumn = new TableColumn();
            surnameColumn.setText("Nazwisko");
            surnameColumn.setCellValueFactory(new PropertyValueFactory("surname"));

            TableColumn emailColumn = new TableColumn();
            emailColumn.setText("Email");
            emailColumn.setCellValueFactory(new PropertyValueFactory("email"));

            TableColumn phoneColumn = new TableColumn();
            phoneColumn.setText("Telefon");
            phoneColumn.setCellValueFactory(new PropertyValueFactory("phoneNumber"));

            TableColumn addressColumn = new TableColumn();
            addressColumn.setText("Adres");
            addressColumn.setCellValueFactory(new PropertyValueFactory("adress"));

            tableView = new TableView();
            tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
            tableView.getColumns().addAll(labelColumn, nameColumn, surnameColumn,
                    emailColumn, phoneColumn, addressColumn);
            root.setCenter(tableView);
        }
        contactListController.loadList();
        stage.show();
    }

    public void setContacts(List<Contact> contacts) {
        tableView.setItems(FXCollections.<Contact>observableList(contacts));
    }
}
